var http = require("../../utils/util")
Page({
// data
  data: {
    background: 'demo-text-1',
    //是否采用衔接滑动  
    circular: true,
    //是否显示画板指示点  
    indicatorDots: true,
    //选中点的颜色  
    indicatorcolor: "#000",
    //是否竖直  
    vertical: false,
    //是否自动切换  
    autoplay: true,
    //自动切换的间隔
    interval: 2500,
    //滑动动画时长毫秒  
    duration: 100,
    //所有图片的高度  
    imgheights: [],
    //图片宽度 
    imgwidth: 750,
    //默认  
    current: 0,
    carousel:[
      {img:"https://img10.360buyimg.com/ddimg/jfs/t1/145501/20/10357/790400/5f816c06E3a86ad99/9a5b436a005cd47e.png"},
      {img:"https://img13.360buyimg.com/ddimg/jfs/t1/129614/34/11950/441556/5f816c04E74fe2181/e00f0dd987707c34.png"},
      {img:"https://img10.360buyimg.com/ddimg/jfs/t1/111895/26/19872/356523/5f816c04E1ebfe070/7577212c1c20ba19.png"},
      {img:"https://img10.360buyimg.com/ddimg/jfs/t1/135635/36/12015/501666/5f816d99E03bd6389/b328c8096077e24c.png"}
    ],
    token:'',
    num: true,
    products:[
    ],
    newProducts: [],
    recommendList: [],//推荐列表
    newList:[],//新品列表
    hotSellList:[]//热销列表
  },
  // --------------------------------------------\
  // model:function(e){     //方法模版
  //   var that = this
  // function Fn(回调函数形参) {
      //回调函数内容
  // }
  //   http.myRequest("/api/goodInfo",
  // {
  // data1:""
  // data2:""
  // data3:""
  // data4:""
  // }
  // ,Fn)
  // },
  //------------------------------------------------------
  // 后端数据格式：
  //   good_id: 1
  // good_name: "乐视（Letv）超级电视 Y43 43英寸 1GB+8GB大存储 人工智能全高清LED平板液晶网络超薄电视机"
  // good_type: ""
  // type_parent_id: 1
  // type_id: 8
  // channel_id: 0
  // price: "999.00"
  // promotion_price: "800.00"
  // img: "http://api.wanxikeji.cn/app/pic/GpG7nFRwJHmqBUst7vuEFYcc6jfneBpoYPbbLWnV.jpeg"
  // admin_id: 0
  // create_time: 1573109777
  // update_time: 1573109777
  // status: 2
  // is_new: 0
   // -------------------部分功能（已经测试过）-------------------------\
  simulation:function(e){//模拟后台上架新酒
    var that = this
    http.myRequest("/api/admin/addModifyGood",{
      token:that.data.token,
      good_name:"国窖1573",
      type_id:"910",
      type_parent_id:"911",
      price:"300.00",
      img:"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1849427806,1639082937&fm=26&gp=0.jpg",//封面
      imgs:"https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1849427806,1639082937&fm=26&gp=0.jpg",//图片集合
      info:"原窖特区泸酒浓香型白酒",//详情，类似简介
      is_new:"1",//是否热卖
      colour:"red",
      edition:"数组保存,pic图片,price价格,sku分类",
      good_id:"001"
    },addGoods)
    function addGoods(_data) {
      console.log(_data);
    }
  },
  commodityMsg:function(e){//查看单条商品信息(购买和结算页面)
    var that = this
    http.myRequest("/api/goodInfo",{good_id:1},query)
    function query(_data) {
      console.log(_data);
      //_data.data为商品信息
    }
  },
  commodityList:function(e){//查看商品列表(购买和结算页面)
    var that = this
    http.myRequest("/api/goodList",{
      price:[100,3600],
      is_new:0
    },query)  
    function query(_data) { //首页初始加载调用
      console.log(_data.data.data);//数组形式商品列表
      that.setData({ 
        recommendList:_data.data.data
      })
    }
  },
  
  // commodityMsg:function(e){//查看单条商品信息(购买和结算页面)
  //   var that = this
  //   http.myRequest("/api/goodInfo",{good_id:1},query)
  //   function query(_data) {
  //     //_data.data为商品信息
  //   }
  // },
  
  //----------------------------------------------------
  BuyItNow:function(e){
      let id = e.currentTarget.dataset['id']
      wx.setStorageSync('shopId', id);
      wx.navigateTo({
        url: '../details/details',
      })
  },
  imageLoad: function (e) {//获取图片真实宽度  
    var imgwidth = e.detail.width,
      imgheight = e.detail.height,
      //宽高比  
      ratio = imgwidth / imgheight;
    //计算的高度值  
    var viewHeight = 750 / ratio;
    var imgheight = viewHeight;
    var imgheights = this.data.imgheights;
    //把每一张图片的对应的高度记录到数组里  
    imgheights[e.target.dataset.id] = imgheight;
    this.setData({
      imgheights: imgheights
    })
  },
  tokenFn:function(){   //页面加载获取token
    var that = this
    wx.getStorage({
      key: 'token',
      success(res){
        that.setData({
          token:res.data
        })  //拿到token之后再次传入回调,请求地址列表,作用域问题只能使用that
        that.addList()
      }
    })
  },
   addList:function(){//接收地址列表。只负责接收到地址
    var that = this
    if( that.data.token !=  ''){
        http.myRequest("/api/userAddressList",{token:that.data.token},listf)
    }else{
      console.log("请先授权！"); 
    }
    function listf(_list){
      var list = _list.data
      // console.log(list);  
      that.setData({
       shippingAdd:list
      })
      wx.setStorageSync('add', that.data.shippingAdd)
    }   
  },
  commodityFn(){
       
  },
  // 从后端请求数据
  getData(){
    wx.request({
      url: 'http://api_dev.wanxikeji.cn/api/goodList',
      method: 'POST',
      data: {
        token:wx.getStorageSync("token"),
      },
      success: res => {
        // console.log(res)
        let data = []
        if (res.statusCode == 200) {
          data = res.data.data.data
        }
        let products = []
        let newProducts = []
        data.forEach(item => {
          if (item.is_new == 1){
            products.push(item)
          }else if(item.is_new == 0){
            newProducts.push(item)
          }
        })
        this.setData(
          {
            newProducts,
            products
          }
        )
        wx.hideToast();
      },
    })
  },
  addCart(e){
    console.log(e);
  },
  /*  生命周期函数--监听页面加载  */
  onLoad: function (options) {
    wx.showToast({title: '加载中', icon: 'loading', duration: 10000});
    this.tokenFn()  //请求token
    this.getData() //请求数据
    let app = getApp()
    app.scanCart(this)
  },

  /* 生命周期函数--监听页面初次渲染完成 */
  onReady: function () {

  },

  /*  生命周期函数--监听页面显示 */
  onShow: function () {
    let app = getApp()
    app.scanCart(this)
    wx.request({
      url: 'http://api_dev.wanxikeji.cn/api/userAddressList',
      method:'POST',
      data: {
          token:wx.getStorageSync("token"),
      },
      success: (res) => {
      }
  });
  },

  /*  生命周期函数--监听页面隐藏  */
  onHide: function () {

  },

  /*  生命周期函数--监听页面卸载 */
  onUnload: function () {

  },

  /*  页面相关事件处理函数--监听用户下拉动作 */
  onPullDownRefresh: function () {

  },

  /*页面上拉触底事件的处理函数 */
  onReachBottom: function () {

  },

  /* 用户点击右上角分享 */
  onShareAppMessage: function () {
    return {
      title: '首页',
      path: 'page/component/pages/swiper/swiper'

    }
  },

  onPageScroll: function () {

  },

  onTabItemTap: function (item) {

  },
  /**
   * 界面加载数据渲染
   */

}) 